﻿using System;
using System.Linq;
using System.Collections.Generic;
using RESTClientLibrary;
using WCFCollection;

namespace RemoteLinqBase.ClientBase
{
	/// <summary>
	/// Factory for storing registered repositories
	/// </summary>
	public static class CollectionsFactory
	{
		private static Dictionary<Type, ClientCollection> _RegisteredCollections = new Dictionary<Type, ClientCollection>();

		private static Type[] _RegisteredTypes = new Type[]{};

		/// <summary>
		/// Registers the collection.
		/// </summary>
		/// <typeparam name="T">Type of item.</typeparam>
		/// <param name="EndPointAddress">The end point address.</param>
		public static void RegisterCollection<T>(string EndPointAddress)
		{
			_RegisteredCollections.Add(typeof(T), 
				new ClientCollection(typeof(T).Name, EndPointAddress));
            SerializationTool.AddNewRegisteredType(typeof(T));
		}

		/// <summary>
		/// Gets the collection.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns>Client collection for specified type.</returns>
		internal static ClientCollection GetCollection(Type type)
		{
			if (!_RegisteredCollections.ContainsKey(type))
			{
				throw new ArgumentException(string.Format("Type {0} not registered as client colection!", type));
			}
			return _RegisteredCollections[type];
		}
	}
}
